package com.gettec.lims.client.bind;

import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.gettec.lims.client.ui.field.BooleanField;

/**
* <p>FieldBinding is broken for RadioGroup fields. This fixes it.</p>
* 
* <p>The bug is that RadioGroup's value is a Radio type, but all
* models will have a String type. This converts between the two.
* Really, RadioGroup's value should be a String type, associated
* with the valueAttribute property.</p> 
*/
public class RadioGroupBinding extends FieldBinding
{
	 public RadioGroupBinding(RadioGroup group, String property) {
		    super(group, property);
		  }

		  public RadioGroupBinding(RadioGroup group) {
		    this(group, group.get(0).getName());
		  }

		  @Override
		  protected Object onConvertModelValue(Object value) {
		    value = super.onConvertModelValue(value);
		    for (Field rad : ((RadioGroup) this.field).getAll()) {
		    	String rediov=((Radio) rad).getValueAttribute();
		    	Boolean redb=Boolean.valueOf(rediov);
		      if (redb.equals(value)) {
		        return rad;
		      }
		    }
		    return null;
		  }

		  @Override
		  protected Object onConvertFieldValue(Object value) {
		    value = super.onConvertFieldValue(value);
		    String v=((Radio)value).getValueAttribute();
		    return  Boolean.valueOf(v);
		  }

}